﻿#region Inlcudes
using System;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using Color4 = OpenTK.Graphics.Color4;
#endregion

namespace GingerGL.Graphics
{
    //------------------------------------------------------------//
    /// <summary>
    /// Render State
    /// </summary>
    public class RenderState
    {
        //--------------------------------------------------------//
        #region Variables
        /// <summary>
        /// Gets/Sets Clear Color
        /// </summary>
        public Color4 ClearColor { get; set; }
        /// <summary>
        /// Gets Current Viewport
        /// </summary>
        public Viewport Viewport { get; private set; }
        /// <summary>
        /// Gets/Sets Clear Buffer Flags
        /// </summary>
        public ClearBufferMask ClearBufferMask { get; set; }
        /// <summary>
        /// Gets/Sets Project Type
        /// </summary>
        public ProjectionType ProjectionType { get; set; }
        #endregion
        //--------------------------------------------------------//
        #region Constructors
        /// <summary>
        /// Initializes a new instance of object
        /// </summary>
        public RenderState()
        {
            this.ClearColor = Color4.Teal;
            this.Viewport = new Viewport();
            this.ClearBufferMask = OpenTK.Graphics.OpenGL.ClearBufferMask.ColorBufferBit | OpenTK.Graphics.OpenGL.ClearBufferMask.DepthBufferBit;
            this.ProjectionType = Graphics.ProjectionType.Perspective;
        }
        #endregion
        //--------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Resize Viewport
        /// </summary>
        public void Resize()
        {
            if (Engine.Instance.GameWindow.Width != 0 && Engine.Instance.GameWindow.Height != 0)
            {
                SetViewport(Engine.Instance.GameWindow.Width, Engine.Instance.GameWindow.Height);
                SetProjection(ProjectionType.Perspective);
            }
        }
        /// <summary>
        /// Set Viewport to Specific Size
        /// </summary>
        public void SetViewport(int Width, int Height)
        {
            this.Viewport.AspectRatio = Width / (float)Height;
            GL.Viewport(0, 0, Width, Height);
        }
        /// <summary>
        /// Sets Projection Mode
        /// </summary>
        public void SetProjection(ProjectionType typeProjection)
        {
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();

            switch (typeProjection)
            {
                case ProjectionType.Perspective:
                    Matrix4 p = Matrix4.CreatePerspectiveFieldOfView
                    (
                        MathHelper.DegreesToRadians(this.Viewport.FieldOfView), 
                        this.Viewport.AspectRatio, 
                        this.Viewport.NearClipdistance, 
                        this.Viewport.FarClipDistance
                    );
                    GL.LoadMatrix(ref p);
                    GL.MatrixMode(MatrixMode.Modelview);
                    GL.LoadIdentity();
                    break;
                case ProjectionType.Ortho2D:
                    Engine.Ortho2D(
                        0, 
                        Engine.Instance.GameWindow.Width, 
                        Engine.Instance.GameWindow.Height, 
                        0);
                    break;
            }
        }
        #endregion
        //--------------------------------------------------------//
    }
    //------------------------------------------------------------//
}
